extends Control
@onready var main_bag: bag = %Bag
@onready var equipment_bag: EquipmentBag = %EquipmentBag
@onready var level: RichTextLabel = %level
@onready var info_2: RichTextLabel = %info2
@onready var info_3: RichTextLabel = %info3
@onready var animation_player: AnimationPlayer = %AnimationPlayer
@onready var close_button: Button = %关闭


var item_db:ItemDataBase = preload("res://scenes/stages/player_bag_and_info/data/item_db.tres")
var player_stats: PandoraEntity

func _ready():
	item_db.load_from_csv("res://scenes/stages/player_bag_and_info/data/wuping_csv.txt")
	main_bag.bag_space.item_data_base = item_db
	EventBus.subscribe("add_item", on_add_item)
	## 订阅打开信号
	EventBus.subscribe("player_bag_show", on_player_bag_show)
	## 订阅打开信号
	EventBus.subscribe("player_bag_hide", on_player_bag_hide)
	player_stats = Utils.player.player_stats
	player_stats.changed.connect(on_player_stats_change)
	on_player_stats_change()

func _unhandled_key_input(event: InputEvent) -> void:
	## 额外判断下close_button的可见性来判断是不是跟随商店打开的
	if event.is_action_pressed("player_bag_show") and close_button.visible == true:
		## 如果在播放动画就不处理
		if animation_player.current_animation != "":
			return
		if visible == true:
			animation_player.play("hide")
		else:
			animation_player.play("show")
					
func _on_关闭_button_down() -> void:
	animation_player.play("hide")

func on_player_stats_change():
	update_basic_info_1()
	update_basic_info_2()
	update_basic_info_3()

func update_basic_info_1():
	var text = ''
	text += "当前等级:[color=yellow] " + str(player_stats.get_integer("level")) + "[/color]\n"
	level.text = text

func update_basic_info_2():
	var text = ''
	text += "最大血量:[color=yellow]" + str(player_stats.get_float("max_hp")) + "[/color]\n"
	text += "最大蓝量:[color=yellow]" + str(player_stats.get_float("max_mp")) + "[/color]\n"
	
	text += "移动速度:[color=yellow]" + str(player_stats.get_integer("move_speed")) + "[/color]"
	if player_stats.get_float("additional_move_speed") > 0:
		text += "[color=green](+" + str(player_stats.get_float("additional_move_speed")) + ")[/color]"
	text += "\n"
	
	text += "基础伤害:[color=yellow]" + str(player_stats.get_float("base_damage")) + "[/color]"
	if player_stats.get_float("additional_damage") > 0:
		text += "[color=green](+" + str(player_stats.get_float("additional_damage")) + ")[/color]"
	text += "\n"
	
	text += "攻击速度:[color=yellow]" + str(player_stats.get_float("attack_speed")) + "[/color]"
	if player_stats.get_float("additional_attack_speed") > 0:
		text += "[color=green](+" + str(player_stats.get_float("additional_attack_speed")) + ")[/color]"
	if player_stats.get_float("additional_attack_speed") < 0:
		text += "[color=black](" + str(player_stats.get_float("additional_attack_speed")) + ")[/color]"
	text += "\n"
	
	text += "冷却缩减:[color=yellow]" + str(roundi((player_stats.get_float("cooldown_reduction") * 100))) + "%[/color]\n"
	text += "血量恢复:[color=yellow]" + str(snapped(player_stats.get_float("health_regen"), 0.01)) + "[/color]\n"
	info_2.text = text

func update_basic_info_3():
	var text = ''
	text += "加攻击力概率:[color=yellow]" + str(roundi(player_stats.get_float("add_attack_probability") * 100)) + "%[/color]\n"
	text += "加血量概率:[color=yellow]" + str(roundi(player_stats.get_float("add_hp_probability") * 100)) + "%[/color]\n"
	text += "加蓝量概率:[color=yellow]" + str(roundi(player_stats.get_float("add_mp_probability") * 100)) + "%[/color]\n"
	text += "加金币概率:[color=yellow]" + str(roundi(player_stats.get_float("add_coin_probability") * 100)) + "%[/color]\n"
	text += "加经验概率:[color=yellow]" + str(roundi(player_stats.get_float("add_exp_probability") * 100)) + "%[/color]\n"
	text += "暴击概率:[color=yellow]" + str(player_stats.get_integer("critical_chance")) + "%[/color]\n"
	text += "蓝量恢复:[color=yellow]" + str(snapped(player_stats.get_float("mp_regen"), 0.01)) + "[/color]\n"
	info_3.text = text

## 购买装备会自动加入，优先装备，其次背包(item_icon_name为触发的那个名字)
func on_add_item(item_stats: PandoraEntity, item_icon_name = '') -> bool:
	var item_dict = Utils.get_item_info(item_stats)
	item_db.force_add_item(item_dict)
	#item_db.save_to_csv("res://scenes/stages/player_bag_and_info/data/wuping_csv.txt")
	#item_db.save_as_resource("res://scenes/stages/player_bag_and_info/data/item_db.tres")

	if equipment_bag.synthesis(item_stats):
		EventBus.push_event("add_item_result", [true, item_icon_name])
		return true
	if main_bag.synthesis(item_stats):
		EventBus.push_event("add_item_result", [true, item_icon_name])
		return true
	if equipment_bag.add_item(item_dict, item_stats):
		EventBus.push_event("add_item_result", [true, item_icon_name])
		return true
	if main_bag.bag_space_add_item(item_stats):
		EventBus.push_event("add_item_result", [true, item_icon_name])
		return true
		
	EventBus.push_event("add_item_result", [false, item_icon_name])
	Utils.show_tip("背包已满")
	return false

## 商店打开时，一同打开，关闭按钮隐藏
func on_player_bag_show():
	animation_player.play("show")
	close_button.visible = false

func on_player_bag_hide():
	animation_player.play("hide")
	await animation_player.animation_finished
	close_button.visible = true
